bitkeeper revision 1.1662.1.10 (42a48d2dOYGp10ZpkS7A_bvbZcKyOw)
authorcl349@firebug.cl.cam.ac.uk <cl349@firebug.cl.cam.ac.uk>
Mon, 6 Jun 2005 17:51:41 +0000 (17:51 +0000)
committercl349@firebug.cl.cam.ac.uk <cl349@firebug.cl.cam.ac.uk>
Mon, 6 Jun 2005 17:51:41 +0000 (17:51 +0000)
XendDomainInfo.py, XendDomain.py:
  Add uuids for domains.
uuid.py:
  new file
Signed-off-by: Mike Wray <mike.wray@hp.com>
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
.rootkeys
tools/python/xen/xend/XendDomain.py
tools/python/xen/xend/XendDomainInfo.py
tools/python/xen/xend/uuid.py [new file with mode: 0644]

index c0914886f0bf974cd9407979e12648243e59af44..3759be63f91f6ffb7fc0c01dafcbb00baa6ccc7d 100644 (file)
--- a/.rootkeys
+++ b/.rootkeys
 4294a1bf8rMUcddot-B2-pOxORimOg tools/python/xen/xend/server/relocate.py
 41ee5e8dq9NtihbL4nWKjuSLOhXPUg tools/python/xen/xend/server/usbif.py
 40c9c469LNxLVizOUpOjEaTKKCm8Aw tools/python/xen/xend/sxp.py
+42a48d152jkT7ykQT_LWKnS-ojV_ZA tools/python/xen/xend/uuid.py
 40d05079aFRp6NQdo5wIh5Ly31c0cg tools/python/xen/xm/__init__.py
 40cf2937gKQcATgXKGtNeWb1PDH5nA tools/python/xen/xm/create.py
 40f552eariuUSB9TWqCPnDLz5zvxMw tools/python/xen/xm/destroy.py
index eed2e7f8a3f59cc3c1db7f4777af27617c04a3e2..5819a9e4bf6e9dee43a28f0e866c3a322054627f 100644 (file)
@@ -125,7 +125,8 @@ class XendDomain:
         @param info:      domain info from xen
         @return: domain
         """
-        dominfo = XendDomainInfo.recreate(savedinfo, info)
+        uuid = sxp.child_value(savedinfo, 'uuid')
+        dominfo = XendDomainInfo.recreate(savedinfo, info, uuid)
         self.domains[dominfo.id] = dominfo
         self.sync_domain(dominfo)
         return dominfo
@@ -295,7 +296,8 @@ class XendDomain:
         @param vmconfig: vm configuration
         """
         config = sxp.child_value(vmconfig, 'config')
-        dominfo = XendDomainInfo.restore(config)
+        uuid = sxp.child_value(vmconfig, 'uuid')
+        dominfo = XendDomainInfo.restore(config, uuid=uuid)
         self._add_domain(dominfo)
         return dominfo
 
@@ -329,7 +331,7 @@ class XendDomain:
                 info = self.xen_domain(id)
                 if info:
                     log.info("Creating entry for unknown domain: id=%d", id)
-                    dominfo = XendDomainInfo.recreate(None, info, unknown=True)
+                    dominfo = XendDomainInfo.recreate(None, info)
                     self._add_domain(dominfo)
             except Exception, ex:
                 log.exception("Error creating domain info: id=%d", id)
index dfce49d9e60f036e5f2565f5d4173d311d283902..257ee38c4f89193bc422e9e9404ef3ad09c024d2 100644 (file)
@@ -29,6 +29,8 @@ from xen.xend.XendLogging import log
 from XendError import XendError, VmError
 from xen.xend.XendRoot import get_component
 
+from xen.xend.uuid import getUuid
+
 """Flag for a block device backend domain."""
 SIF_BLK_BE_DOMAIN = (1<<4)
 
@@ -143,12 +145,16 @@ class XendDomainInfo:
     """
     MINIMUM_RESTART_TIME = 20
 
-    def _create(cls):
-        """Create a vm object.
+    def _create(cls, uuid=None):
+        """Create a vm object with a uuid.
 
+        @param uuid uuid to use (generated if None)
         @return vm
         """
+        if uuid is None:
+            uuid = getUuid()
         vm = cls()
+        vm.uuid = uuid
         return vm
 
     _create = classmethod(_create)
@@ -167,17 +173,14 @@ class XendDomainInfo:
 
     create = classmethod(create)
 
-    def recreate(cls, savedinfo, info, unknown=False):
+    def recreate(cls, savedinfo, info, uuid=None):
         """Create the VM object for an existing domain.
 
         @param savedinfo: saved info from the domain DB
         @param info:      domain info from xc
         @type  info:      xc domain dict
         """
-        if unknown:
-            vm = cls._create()
-        else:
-            vm = cls()
+        vm = cls._create(uuid=uuid)
 
         log.debug('savedinfo=' + prettyprintstring(savedinfo))
         log.debug('info=' + str(info))
@@ -209,12 +212,12 @@ class XendDomainInfo:
 
     recreate = classmethod(recreate)
 
-    def restore(cls, config):
+    def restore(cls, config, uuid=None):
         """Create a domain and a VM object to do a restore.
 
         @param config:    domain configuration
         """
-        vm = cls._create()
+        vm = cls._create(uuid=uuid)
         dom = xc.domain_create()
         vm.setdom(dom)
         vm.dom_construct(vm.id, config)
@@ -227,6 +230,7 @@ class XendDomainInfo:
         self.restore = 0
         
         self.config = None
+        self.uuid = None
         self.id = None
         self.cpu_weight = 1
         self.start_time = None
@@ -365,7 +369,8 @@ class XendDomainInfo:
                 ['id', self.id],
                 ['name', self.name],
                 ['memory', self.memory] ]
-
+        if self.uuid:
+            sxpr.append(['uuid', self.uuid])
         if self.info:
             sxpr.append(['maxmem', self.info['maxmem_kb']/1024 ])
             run   = (self.info['running']  and 'r') or '-'
diff --git a/tools/python/xen/xend/uuid.py b/tools/python/xen/xend/uuid.py
new file mode 100644 (file)
index 0000000..096fef7
--- /dev/null
@@ -0,0 +1,65 @@
+"""Universal(ly) Unique Identifiers (UUIDs).
+"""
+import commands
+import random
+
+def uuidgen(random=True):
+    """Generate a UUID using the command uuidgen.
+
+    If random is true (default) generates a random uuid.
+    If random is false generates a time-based uuid.
+    """
+    cmd = "uuidgen"
+    if random:
+        cmd += " -r"
+    else:
+        cmd += " -t"
+    return commands.getoutput(cmd)
+
+class UuidFactoryUuidgen:
+
+    """A uuid factory using uuidgen."""
+
+    def __init__(self):
+        pass
+
+    def getUuid(self):
+        return uuidgen()
+
+class UuidFactoryRandom:
+
+    """A random uuid factory."""
+
+    def __init__(self):
+        f = file("/dev/urandom", "r")
+        seed = f.read(16)
+        f.close()
+        self.rand = random.Random(seed)
+
+    def randBytes(self, n):
+        return [ self.rand.randint(0, 255) for i in range(0, n) ]
+
+    def getUuid(self):
+        bytes = self.randBytes(16)
+        # Encode the variant.
+        bytes[6] = (bytes[6] & 0x0f) | 0x40
+        bytes[8] = (bytes[8] & 0x3f) | 0x80
+        f = "%02x"
+        return ( "-".join([f*4, f*2, f*2, f*2, f*6]) % tuple(bytes) )
+
+def getFactory():
+    """Get the factory to use for creating uuids.
+    This is so it's easy to change the uuid factory.
+    For example, for testing we might want repeatable uuids
+    rather than the random ones we normally use.
+    """
+    global uuidFactory
+    try:
+        uuidFactory
+    except:
+        #uuidFactory = UuidFactoryUuidgen()
+        uuidFactory = UuidFactoryRandom()
+    return uuidFactory
+
+def getUuid():
+    return getFactory().getUuid()